package com.base.controller.file;

import com.alibaba.fastjson.JSON;
import com.beust.jcommander.internal.Maps;
import com.base.comm.CommUrls;
import com.base.utils.Produce;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @Date: 2019/11/2 11:51
 * @Description: layui edit 专属控件
 */
@Controller
@RequestMapping("/layfile")
@Slf4j
public class FileUpLoadController {

    // 上传文件
    @RequestMapping(value = "/uploadFile1", method = RequestMethod.POST)
    public @ResponseBody
    String layDownload(HttpServletRequest request, @RequestParam("file") MultipartFile file)
            throws Exception {
        Map<String, Object> map = Maps.newHashMap();
        Map<String, Object> map2 = Maps.newHashMap();
        if (!file.isEmpty()) {

            String fileType = getExtensionName(file.getOriginalFilename());
            if (!"jpg".equals(fileType) && !"jpeg".equals(fileType)
                    && !"png".equals(fileType) && !"gif".equals(fileType)
                    && !"xls".equals(fileType) && !"xlsx".equals(fileType)
                    && !"txt".equals(fileType)) {
                map.put("code", -1);
                map.put("msg", "上传失败:文件类型错误");
                map2.put("src", "");
                map2.put("title", "");
                map.put("data", map2);
                return JSON.toJSONString(map);
            }

            String filename = Produce.getUUID() + "." + fileType;
            String fileReal = CommUrls.FILEPATH + new SimpleDateFormat("yyyy-MM-dd").format(Produce.getNowDate());
            File filepath = new File(fileReal, filename);
            // 判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }

            // 将上传文件保存到一个目标文件当中
            file.transferTo(filepath);
//            if(file.getSize() > 1024 * 1024){
//                log.warn("压缩");
//                Thumbnails.of(filepath).size(300, 200).outputQuality(1.0d).outputFormat(fileType).toFile(filepath);
//            }
            map.put("code", 0);
            map.put("msg", "上传成功");
            map2.put("src", CommUrls.Treaty + CommUrls.Url + "file/" + new SimpleDateFormat("yyyy-MM-dd").format(Produce.getNowDate()) + "/"
                    + filename);
            map2.put("title", "上传的图片");

        } else {
            map.put("code", -1);
            map.put("msg", "上传失败:请选择文件");
            map2.put("src", "");
            map2.put("title", "");
        }
        map.put("data", map2);
        return JSON.toJSONString(map);
    }

    private String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }
}
